package zk.weart.util;

import java.math.BigDecimal;
import java.util.Random;

/**
 * @Auther:
 * @Date: 2019/3/12 15:57
 * @Description:
 */
public class MathUtil {

    /**
     * 返向计算比例  doube
     * @param rate
     * @return
     */
    public static Double percentRate(Double rate ){

        BigDecimal a  = new BigDecimal(new Double(100));


        BigDecimal b  = new BigDecimal(rate);


        BigDecimal c  = a.subtract(b);

        Double d =   c.divide(new BigDecimal(100)).doubleValue();

        return d;

    }

    /**
     * 货币转化
     * double 转换成 long
     * 圆转换成分
     * @return
     */
    public static Long transForMoney(Double money){

        Double c =  money*100;

        BigDecimal b  = new BigDecimal(c);
        return  b.longValue();

    }


    /**
     * long 转换成 double
     * @param money
     * @return
     */
    public static Double transForMoney(Long money){

        BigDecimal a  = new BigDecimal(new Double(100));

        BigDecimal b  = new BigDecimal(money);
        return  b.divide(a).doubleValue();

    }

    /**
     * 保留两位小数
     * @param amount
     * @return
     */
    public static Double transScaleMoney(Long amount) {
         String money= new BigDecimal(amount).divide(new BigDecimal(100)).setScale(2).toString();
        return  new Double(money);
    }

    public static Double transScaleMoney(Long amount,double rate) {
        BigDecimal money =   new BigDecimal(amount).divide(new BigDecimal(100));

        BigDecimal m = money.multiply(BigDecimal.valueOf(rate));

        BigDecimal c = m.setScale(2,BigDecimal.ROUND_FLOOR);

       return  c.doubleValue();
    }


    public static BigDecimal transBigDecimal(Long amount,Double rate) {
        BigDecimal money =   new BigDecimal(amount);

        BigDecimal m = money.multiply(BigDecimal.valueOf(rate));

        BigDecimal c = m.setScale(2,BigDecimal.ROUND_FLOOR);

        return c;
    }

    /**
     * 生成指定范围的随机数
     * @return
     */
    public static Integer getRandom(int min,int max){
        return  new Random().nextInt(max-min)+min;
    }

    public static void main(String[] args) {

        for(int i =0;i<100;i++){
            System.out.println(getRandom(0,4));

        }

    }


}
